/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.pdfconverter;

import com.itextpdf.text.Document;
import java.awt.Color;
import java.util.logging.Level;
import javax.swing.filechooser.FileNameExtensionFilter;
import project.jpdfexport.constants.JPDFConst.ALIGNMENT;
import project.jpdfexport.constants.JPDFConst.ORIENTATION;
import project.jpdfexport.constants.JPDFConst.PAPER_SIZE;
import project.jpdfexport.constants.JPDFConst.STYLE;
import project.jpdfexport.constants.JPDFConst.TYPE;
import project.jpdfexport.font.FontFactory;
import project.jpdfexport.jpdf.JPDFExport;
import project.jpdfexport.jpdf.PDFText;
import project.jpdftools.constants.Const;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.managers.other.FileUtil;

/**
 *
 * @author Mido
 */
public class TextToPdf extends Converter {

    public TextToPdf() {
        super();
        // set extensions
        this.setSourceExtensionFilter(new FileNameExtensionFilter("Text Files (*.txt)", "txt"));
        this.setDestinationExtension(".pdf");
        this.setUseAdvancedOptions(true);
        // use left-to-right languages as the default language
        this.setLanguageType(TYPE.left_to_right);
        // default font used
        this.setUseDefaultFont(true);
        // use document info
        this.setUseDocumentInfo(true);
        LoggerManager.getLogger().log(Level.INFO, "Text-to-Pdf conversion selected.");
    }

    @Override
    public boolean convertFiles(String creator, String title, String author, String subject, String keywords, String font_path_name) {
        Document document = null;
        try {
            // read the text from the file
            String text = "";
            if (this.getLanguageType() == TYPE.left_to_right) {
                text = new FileUtil().readALlDataUsingEncoding(this.getSource(), "UTF8");
            } else {
                text = new FileUtil().readALlDataUsingEncoding(this.getSource(), "cp1256");
            }

            // Document object to hold the data
            document = new Document();

            // export object
            JPDFExport export_object = new JPDFExport();

            // set the document properties before opening it
            document = export_object.prepareDocument(document, PAPER_SIZE.A4, ORIENTATION.Portrait, true, 0, 0, 0, 0);

            // open the document
            document = export_object.openDocument(document, this.getDestination());

            // set document description
            document = export_object.setDocumentDescription(document, title, author, subject, keywords, Const.program_version);

            if (this.isUseDefaultFont() == false) {
                PDFText.addText(document, text, font_path_name, 0, Color.white, false, 12,
                        Color.black, STYLE.normal, ALIGNMENT.left, Color.white);
            } else {
                PDFText.addText(document, text, FontFactory.getDefaultFontPath(), 0, Color.white, false, 12,
                        Color.black, STYLE.normal, ALIGNMENT.left, Color.white);
            }
            // close the document
            document.close();
            LoggerManager.getLogger().log(Level.INFO, "File converted successfully.");
            return true;
        } catch (Exception ex) {
            LoggerManager.getLogger().log(Level.SEVERE, "Error in file conversion. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(ex.getCause()));
            if (document != null) {
                // close the document
                document.close();
            }
            return false;
        }
    }
}
